package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pick.PrePickNoticeDetailEntity;
import com.sgb.link.view.widget.ExpandedTextView;
import com.swgk.core.recyclerview.secondary.CommonHolder;

/**
 * Created by wj on 2021/2/5 0005
 * Description：
 */
public class PickNoticeDetailsAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private PrePickNoticeDetailEntity.NoticeInfoBean noticeInfoBean;
    protected OnItemClick onItemClick;
    private boolean showEngineeringCategory;
    private boolean showEngineeringMode;
    private boolean showSiteSurvey;
    private boolean showSiteSurveyTime;

    public PickNoticeDetailsAdapter(Context context, PrePickNoticeDetailEntity pickNoticeDetailEntity) {
        this.mContext = context;
        this.noticeInfoBean = pickNoticeDetailEntity.getNoticeInfo();

        //1.当招标类型为“4专业工程”、“2劳务分包”时，新增字段：工程规模、是否组织踏勘、踏勘时间、集中地点
        //2.当招标类型为“6勘察设计”、“7建筑设计”、“8工程监理”、“9工程造价咨询”、“10工程总承包”时，新增字段：工程类别、工程规模、是否组织踏勘、踏勘时间、集中地点
        // （其中，不组织踏勘时，不显示字段：踏勘时间、集中地点）
        int tenderType = noticeInfoBean.getTenderType();
        if (tenderType == 4 || tenderType == 2 || tenderType == 6 || tenderType == 7 || tenderType == 8 || tenderType == 9 || tenderType == 10) {
            if (tenderType == 6 || tenderType == 7 || tenderType == 8 || tenderType == 9 || tenderType == 10) {
                showEngineeringCategory = true;
            }
            showEngineeringMode = true;
            showSiteSurvey = true;
            if (noticeInfoBean.getIsOrganizationSurveySite() == 2) {   //1.不组织；2.组织
                showSiteSurveyTime = true;
            } else {
                showSiteSurveyTime = false;
            }
        } else {
            showEngineeringCategory = false;
            showEngineeringMode = false;
            showSiteSurvey = false;
            showSiteSurveyTime = false;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = CommonHolder.get(mContext, parent, R.layout.tender_details_item);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        initItem((CommonHolder) holder, position);

    }

    private void initItem(CommonHolder holder, int position) {
        View itemView = holder.getItem();
        switch (position) {
            case 0:
                chooseShowItem(itemView, noticeInfoBean.getName(), "招标名称");
                break;
            case 1:
                chooseShowItem(itemView, noticeInfoBean.getTenderTypeName(), "招标类型");
                break;
            case 2:
                if (TextUtils.isEmpty(noticeInfoBean.getFullAddress())) {
                    itemView.findViewById(R.id.ll_full_address).setVisibility(View.GONE);
                } else {
                    itemView.findViewById(R.id.ll_full_address).setVisibility(View.VISIBLE);
                    String lat = noticeInfoBean.getLat();
                    String lng = noticeInfoBean.getLng();
                    String address = noticeInfoBean.getFullAddress();
                    TextView addressTitle = itemView.findViewById(R.id.address_title);
                    if (noticeInfoBean.getTenderType() == 12 || noticeInfoBean.getTenderType() == 11) {
                        addressTitle.setText("使用地址");
                    } else if (noticeInfoBean.getTenderType() == 3) {
                        addressTitle.setText("收货地址");
                    } else if (noticeInfoBean.getTenderType() == 5 || noticeInfoBean.getTenderType() == 1) {
                        addressTitle.setText("工作地址");
                    } else if (noticeInfoBean.getTenderType() == 14) {
                        addressTitle.setText("土地地址");
                    } else if (noticeInfoBean.getTenderType() == 15 || noticeInfoBean.getTenderType() == 16 || noticeInfoBean.getTenderType() == 17 || noticeInfoBean.getTenderType() == 18 || noticeInfoBean.getTenderType() == 20) {
                        addressTitle.setText("所在地址");
                    } else if (noticeInfoBean.getTenderType() == 19) {
                        addressTitle.setText("发货地址");
                    } else {
                        addressTitle.setText("项目地址");
                    }
                    TextView addressContent = itemView.findViewById(R.id.address_content);
                    addressContent.setText(address);
                    ImageView imageViewMap = itemView.findViewById(R.id.pick_location_map);
                    if (!TextUtils.isEmpty(lat) && !TextUtils.isEmpty(lng)) {
                        imageViewMap.setVisibility(View.VISIBLE);
                    }

                    String distance = TextUtils.isEmpty(noticeInfoBean.getDistance()) ? "" : noticeInfoBean.getDistance();
                    TextView tvDistance = itemView.findViewById(R.id.tv_distance);
                    if (!TextUtils.isEmpty(distance)) {
                        tvDistance.setText(distance + "km");
                    } else {
                        tvDistance.setText("");
                    }
                    tvDistance.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onItemClick.onDistanceClick(lat, lng, address);
                        }
                    });
                    imageViewMap.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onItemClick.onMapClick(lat, lng, address);
                        }
                    });
                }
                itemView.findViewById(R.id.item_layout).setVisibility(View.GONE);
                itemView.findViewById(R.id.ll_survey_view).setVisibility(View.GONE);
                itemView.findViewById(R.id.ll_contacts).setVisibility(View.GONE);
                itemView.findViewById(R.id.layout_time).setVisibility(View.GONE);
                break;
            case 3:
                if (showEngineeringCategory) {
                    chooseShowItem(itemView, noticeInfoBean.getProjectCategoryName(), "工程类别");
                } else {
                    itemView.findViewById(R.id.item_layout).setVisibility(View.GONE);
                    goneView(itemView);
                }
                break;
            case 4:
                chooseShowItem1(itemView, noticeInfoBean.getRemark(), "招标概况");
                break;
            case 5:
                if (showEngineeringMode) {
                    chooseShowItem1(itemView, noticeInfoBean.getProjectScale(), "工程规模");
                } else {
                    itemView.findViewById(R.id.ll_survey_view).setVisibility(View.GONE);
                    goneView1(itemView);
                }
                break;
            case 6:
                if (showSiteSurvey) {
                    String siteSurvey = "";
                    if (noticeInfoBean.getIsOrganizationSurveySite() == 2) {   //1.不组织；2.组织
                        siteSurvey = "组织";
                    } else {
                        siteSurvey = "不组织";
                    }
                    chooseShowItem(itemView, siteSurvey, "是否组织\n踏勘现场");
                } else {
                    itemView.findViewById(R.id.item_layout).setVisibility(View.GONE);
                    goneView(itemView);
                }
                break;
            case 7:
                if (showSiteSurveyTime) {
                    chooseShowItem(itemView, noticeInfoBean.getSurveyTime(), "踏勘时间");
                } else {
                    itemView.findViewById(R.id.item_layout).setVisibility(View.GONE);
                    goneView(itemView);
                }
                break;
            case 8:
                if (showSiteSurveyTime) {
                    chooseShowItem1(itemView, noticeInfoBean.getSurveyFocusSite(), "踏勘集中地点");
                } else {
                    itemView.findViewById(R.id.ll_survey_view).setVisibility(View.GONE);
                    goneView1(itemView);
                }
                break;
            case 9:
                chooseShowItem(itemView, noticeInfoBean.getMoney(), "概算金额");
                break;
            case 10:
                chooseShowItem(itemView, noticeInfoBean.getVerticalTypeName(), "招标方式");
                break;
            case 11:
                chooseShowItem(itemView, noticeInfoBean.getApplyTimeRange(), "报名时间");
                break;
//            case 12:
//                chooseShowItem(itemView, noticeInfoBean.getBiddingMethodName(), "招标方式");
//                break;
            case 13:
                chooseShowItem(itemView, noticeInfoBean.getBiddingDocTimeRange(), "投标时间");
                break;
            case 14:
                String CashDeposit = "";
                if (!TextUtils.isEmpty(noticeInfoBean.getCashDeposit())) {
                    CashDeposit = noticeInfoBean.getCashDeposit() + "元";
                }
                if (noticeInfoBean.getChargeCashDeposit() != 2) {
                    chooseShowItem(itemView, CashDeposit, "投标保证金");
                } else {
                    itemView.findViewById(R.id.item_layout).setVisibility(View.GONE);
                    goneView(itemView);
                }
                break;
            case 15:
                if (noticeInfoBean.getMoreContactList() != null && noticeInfoBean.getMoreContactList().size() > 0) {
                    chooseShowItem(itemView, noticeInfoBean.getContacts() + " " + noticeInfoBean.getTel(), "联系人1");
                }else{
                    chooseShowItem(itemView, noticeInfoBean.getContacts() + " " + noticeInfoBean.getTel(), "联系人");
                }
                break;
            case 16:
                if (noticeInfoBean.getMoreContactList() != null && noticeInfoBean.getMoreContactList().size() > 0) {
                    chooseShowItem(itemView, noticeInfoBean.getMoreContactList().get(0) + " " + noticeInfoBean.getMoreTelList().get(0), "联系人2");
                } else {
                    itemView.findViewById(R.id.item_layout).setVisibility(View.GONE);
                    goneView(itemView);
                }
                break;
            case 17:
                if (noticeInfoBean.getMoreContactList() != null && noticeInfoBean.getMoreContactList().size() > 1) {
                    chooseShowItem(itemView, noticeInfoBean.getMoreContactList().get(1) + " " + noticeInfoBean.getMoreTelList().get(1), "联系人3");
                } else {
                    itemView.findViewById(R.id.item_layout).setVisibility(View.GONE);
                    goneView(itemView);
                }
                break;
            case 18:
                if (noticeInfoBean.getMoreContactList() != null && noticeInfoBean.getMoreContactList().size() > 2) {
                    chooseShowItem(itemView, noticeInfoBean.getMoreContactList().get(2) + " " + noticeInfoBean.getMoreTelList().get(2), "联系人4");
                } else {
                    itemView.findViewById(R.id.item_layout).setVisibility(View.GONE);
                    goneView(itemView);
                }
                break;
            case 19:
                if (noticeInfoBean.getMoreContactList() != null && noticeInfoBean.getMoreContactList().size() > 3) {
                    chooseShowItem(itemView, noticeInfoBean.getMoreContactList().get(3) + " " + noticeInfoBean.getMoreTelList().get(3), "联系人5");
                } else {
                    itemView.findViewById(R.id.item_layout).setVisibility(View.GONE);
                    goneView(itemView);
                }
                break;

        }

    }

    private void chooseShowItem1(View view, String itemValue, String itemTitle) {
        if (TextUtils.isEmpty(itemValue)) {
            view.findViewById(R.id.ll_survey_view).setVisibility(View.GONE);
        } else {
            view.findViewById(R.id.ll_survey_view).setVisibility(View.VISIBLE);

            TextView surveyView = view.findViewById(R.id.survey_view);
            TextView expandView = view.findViewById(R.id.expand_view);
            TextView surveyTitle = view.findViewById(R.id.survey_title);
            surveyTitle.setText(itemTitle);
            surveyView.setText(itemValue);
            new ExpandedTextView().showTextPick(surveyView, expandView);
        }
        goneView1(view);
    }

    private void chooseShowItem(View view, String itemValue, String itemTitle) {
        TextView tVTitle = view.findViewById(R.id.item_title);
        TextView tVContent = view.findViewById(R.id.item_content);
        if (!TextUtils.isEmpty(itemValue)) {
            view.findViewById(R.id.item_layout).setVisibility(View.VISIBLE);
            if (itemTitle.contains("联系人")) {
                SpannableString span = new SpannableString(itemValue);
                ClickableSpan click = new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        String tel  = itemValue.substring(itemValue.length() - 11, itemValue.length());
                        Intent intent = new Intent(Intent.ACTION_DIAL);
                        Uri data = Uri.parse("tel:" + tel);
                        intent.setData(data);
                        mContext.startActivity(intent);
                    }
                    @Override
                    public void updateDrawState(TextPaint ds) {
                        ds.setUnderlineText(false);
                    }
                };
                span.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.color_0286DF)), itemValue.length() - 11, itemValue.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                span.setSpan(click, itemValue.length() - 11, itemValue.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tVContent.append(span);
                tVContent.setMovementMethod(LinkMovementMethod.getInstance());
                tVContent.setTextColor(mContext.getResources().getColor(R.color.color_tab_one));
            }else{
                if (itemTitle.equals("概算金额") || itemTitle.equals("保证金")) {
                    tVContent.setTextColor(mContext.getResources().getColor(R.color.new_org));
                    tVContent.setText(itemValue + "元");
                } else {
                    tVContent.setText(itemValue);
                    tVContent.setTextColor(mContext.getResources().getColor(R.color.color_tab_one));
                }
            }
            tVTitle.setText(itemTitle);
        } else {
            view.findViewById(R.id.item_layout).setVisibility(View.GONE);
        }
        goneView(view);
    }

    private void goneView(View view) {
        view.findViewById(R.id.ll_full_address).setVisibility(View.GONE);
        view.findViewById(R.id.ll_survey_view).setVisibility(View.GONE);
        view.findViewById(R.id.ll_contacts).setVisibility(View.GONE);
        view.findViewById(R.id.layout_time).setVisibility(View.GONE);
    }

    private void goneView1(View view) {
        view.findViewById(R.id.item_layout).setVisibility(View.GONE);
        view.findViewById(R.id.ll_full_address).setVisibility(View.GONE);
        view.findViewById(R.id.ll_contacts).setVisibility(View.GONE);
        view.findViewById(R.id.layout_time).setVisibility(View.GONE);
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return 20;
    }

    public interface OnItemClick {
        void onMapClick(String lat, String lag, String address);
        void onDistanceClick(String lat, String lag, String address);
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }
}
